﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using TempCtr.Model;

namespace TempCtr
{
    #pragma warning disable IDE1006 // 命名样式
    public partial class port_setting : Form
    {

        //SerialPort sp = new SerialPort();

        public port_setting()
        {
            InitializeComponent();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            #region 参数正确性判断
            if (port_num.Text == "")
            {
                MessageBox.Show("请选择正确的COM口", "系统提示");
                return;
            }
            if (!int.TryParse(baud_rate.Text,out var var_baud_rate))
            {
                MessageBox.Show("请输入数字", "系统提示");
                return;
            }
            if (!int.TryParse(time_interval.Text, out var var_time_interval))
            {
                MessageBox.Show("请输入数字", "系统提示");
                return;
            }
            #endregion

            #region 参数保存           
            Properties.Settings.Default.Port_COM = port_num.Text;
            Properties.Settings.Default.Port_BaudRate = var_baud_rate;
            Properties.Settings.Default.Port_Parity = parity_check.Text;
            Properties.Settings.Default.Port_DataBits = data_bit.Text;
            Properties.Settings.Default.Port_StopBits = stop_bit.Text;
            Properties.Settings.Default.Scan_Interval = var_time_interval;
            Properties.Settings.Default.Save();
            #endregion

            MessageBox.Show("设置已保存", "系统提示");
        }

        private void port_setting_Load(object sender, EventArgs e)
        {
            #region 检测串口
            port_num.Items.Clear();
            port_num.Items.AddRange(SerialPort.GetPortNames());
            if (port_num.Items.Count>0)
            {
                port_num.Text = Properties.Settings.Default.Port_COM;
            }
            else
            {
                MessageBox.Show("无可用串口！", "错误提示");
            }
            #endregion

            baud_rate.Text = Properties.Settings.Default.Port_BaudRate.ToString();
            parity_check.Text = Properties.Settings.Default.Port_Parity;
            data_bit.Text = Properties.Settings.Default.Port_DataBits;
            stop_bit.Text = Properties.Settings.Default.Port_StopBits;
            time_interval.Text = Properties.Settings.Default.Scan_Interval.ToString();
        }

        #pragma warning restore IDE1006 // 命名样式
        private void btn_return_Click(object sender, EventArgs e)
        {

            Thermostat.Reconnection();
        }

        #pragma warning disable IDE1006 // 命名样式
        private void port_num_DropDown(object sender, EventArgs e)
        {
            port_num.Items.Clear();
            port_num.Items.AddRange(SerialPort.GetPortNames());
            if (port_num.Items.Count > 0)
            {
                port_num.Text = Properties.Settings.Default.Port_COM;
            }
            else
            {
                MessageBox.Show("无可用串口！", "错误提示");
            }
        }
    }
}
